<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use DB;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function($query) {
//            $this->logSQL($query->sql, $query->time);
//            $this->logBindings($query->bindings);
        });
    }

    /**
     * Log the sql.
     *
     * @return void
     */
    private function logSQL($sql, $time){
        $log = $sql . " [ RunTime:{$time}ms ]";
        \Log::info($log);
    }

    /**
     * Log the sql bindings.
     *
     * @return void
     */
    private function logBindings($bindings){
        if (empty($bindings)) {
            return '';
        }

        $param = [];
        mb_internal_encoding("UTF-8");
        foreach ($bindings as $key => $value) {
            if (mb_strlen($value) > 256) {
                $value = mb_substr($value, 256);
            }
            array_push($param, $value);
        }
        $export = var_export($param, true);
        $log    = str_replace("\n", "", $export);
        \Log::info($log);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
